﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Diagnostics;
using PPI.Client.Logic;
using PPI.Entities.Settings.Mobile;

namespace PPI.Client.Utils
{
    public class SetupHelper
    {
        public static void Setup()
        {
            DirectoryInfo _repositoryDirectory = new DirectoryInfo(Constants.K_RepositoryFolder);
            if (!_repositoryDirectory.Exists)
            {
                _repositoryDirectory.Create();
            }
            if (_repositoryDirectory.GetDirectories(Constants.ErrorFolderName + "*").Length == 0)
                _repositoryDirectory.CreateSubdirectory(Constants.ErrorFolderName);
            if (_repositoryDirectory.GetDirectories(Constants.InboxFolderName + "*").Length == 0)
                _repositoryDirectory.CreateSubdirectory(Constants.InboxFolderName);
            if (_repositoryDirectory.GetDirectories(Constants.SentFolderName + "*").Length == 0)
                _repositoryDirectory.CreateSubdirectory(Constants.SentFolderName);
            if (_repositoryDirectory.GetDirectories(Constants.ImageCacheFolderName + "*").Length == 0)
                _repositoryDirectory.CreateSubdirectory(Constants.ImageCacheFolderName);
            if (_repositoryDirectory.GetDirectories(Constants.OutboxFolderName + "*").Length == 0)
                _repositoryDirectory.CreateSubdirectory(Constants.OutboxFolderName);

            DirectoryInfo tmpDirectory;
            if (_repositoryDirectory.GetDirectories(Constants.TempFolderName + "*").Length == 0)
                tmpDirectory = _repositoryDirectory.CreateSubdirectory(Constants.TempFolderName);
            else
                tmpDirectory = _repositoryDirectory.GetDirectories(Constants.TempFolderName + "*")[0];

            if (tmpDirectory.GetDirectories(Constants.K_INSPECTIONFOLDERNAME+ "*").Length == 0)
                tmpDirectory.CreateSubdirectory(Constants.K_INSPECTIONFOLDERNAME);
            if (tmpDirectory.GetDirectories(Constants.K_GPSFOLDERNAME + "*").Length == 0)
                tmpDirectory.CreateSubdirectory(Constants.K_GPSFOLDERNAME);

            DirectoryInfo tmdDistributionInspectionDirectories = new DirectoryInfo(Constants.TempFolderName + "\\" + Constants.K_DISTRIBUTIONINSPECTIONFOLDERNAME);
            if (!tmdDistributionInspectionDirectories.Exists) 
                tmpDirectory.CreateSubdirectory(Constants.K_DISTRIBUTIONINSPECTIONFOLDERNAME);

            DirectoryInfo tmdDistributionGPSDirectories = new DirectoryInfo(Constants.TempFolderName + "\\" + Constants.K_DISTRIBUTIONGPSFOLDERNAME);
            if (!tmdDistributionGPSDirectories.Exists)
                tmpDirectory.CreateSubdirectory(Constants.K_DISTRIBUTIONGPSFOLDERNAME);

            DirectoryInfo tmdDistributionInspectionMeterDirectories = new DirectoryInfo(Constants.TempFolderName + "\\" + Constants.K_DISTRIBUTIONINSPECTIONMETERFOLDERNAME);
            if (!tmdDistributionInspectionMeterDirectories.Exists)
                tmpDirectory.CreateSubdirectory(Constants.K_DISTRIBUTIONINSPECTIONMETERFOLDERNAME);

            DirectoryInfo tmdDistributionGPSMeterDirectories = new DirectoryInfo(Constants.TempFolderName + "\\" + Constants.K_DISTRIBUTIONGPSMETERFOLDERNAME);
            if (!tmdDistributionGPSMeterDirectories.Exists)
                tmpDirectory.CreateSubdirectory(Constants.K_DISTRIBUTIONGPSMETERFOLDERNAME);
            
            DirectoryInfo _outboxDirectory = new DirectoryInfo(Constants.K_OUTBOXPATH);
            if (_outboxDirectory.GetDirectories(Constants.K_GPSFOLDERNAME + "*").Length == 0)
                _outboxDirectory.CreateSubdirectory(Constants.K_GPSFOLDERNAME);
            if (_outboxDirectory.GetDirectories(Constants.K_INSPECTIONFOLDERNAME + "*").Length == 0)
                _outboxDirectory.CreateSubdirectory(Constants.K_INSPECTIONFOLDERNAME);

            DirectoryInfo _distributionOutboxDirectory = new DirectoryInfo(Constants.K_DISTRIBUTIONINSPECTIONFOLDERNAME);
            if (!_distributionOutboxDirectory.Exists)
                _outboxDirectory.CreateSubdirectory(Constants.K_DISTRIBUTIONINSPECTIONFOLDERNAME);

            _distributionOutboxDirectory = new DirectoryInfo(Constants.K_DISTRIBUTIONGPSFOLDERNAME);
            if (!_distributionOutboxDirectory.Exists)
                _outboxDirectory.CreateSubdirectory(Constants.K_DISTRIBUTIONGPSFOLDERNAME);
        }

        public static void OpenInWebbrownser(string url)
        {
            Process iexProcess = new Process();
            iexProcess.StartInfo.FileName = "iexplore";
            iexProcess.StartInfo.Arguments = url;
            iexProcess.Start();


        }

        public static bool CheckProjectDatabaseDateLessOneDay()
        {
            string connectionString = Constants.K_LOCALDATABASEPROJECTPATH;
            if(File.Exists(connectionString))
            {
                string ProjectLastUpdate = LOutbox.GetSettingNameByKey(connectionString, EnumMobileSettings.LastUpdate);
                DateTime ProjectDate = DateTime.Parse(ProjectLastUpdate);

                if(DateTime.Now.Subtract(ProjectDate).Days > 1)
                {
                    return true;
                }else
                    return false;
            }else
            {
                return false;
            }
        }
    }
}
